/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      QuorumFunctions.cpp
 *
 * $FTS$
 */
#include <OutputDataCollector.hpp>

#include <stdio.h>
#include <stdlib.h>

using namespace FTSS;

class XMLOutputDataCollector:
  public OutputDataCollector
{
private:
	FILE * fOutputStream;
	INT_32 iDocNum;
	INT_64 iSec;
	INT_64 iUSec;
public:

	// Constructor
	XMLOutputDataCollector(FILE * fIOutputStream): fOutputStream(fIOutputStream), iDocNum(0) { ;; }

	// Destructor
	~XMLOutputDataCollector() throw() { ;; }
private:
	/**
	  @brief Start search results collection
	  @param iDocumentsCount - Number of documents
	  @param aTerms - List of terms
	  @param iTermsCount - Munber of terms in list
	*/
	void StartOutput(const INT_32 & iDocumentsCount, const TermStatistic * aTerms, const INT_32 & iTermsCount)
	{
		iDocNum = 0;
		fprintf(fOutputStream, "<?xml version=\"1.0\"?>\n<FTSSP version=\"1.0\">\n");
		fprintf(fOutputStream, "\t<TM>1</TM>\n");
		fprintf(fOutputStream, "\t<TL N=\"%d\">\n", iTermsCount);
		for (INT_32 iI = 0; iI < iTermsCount; ++iI)
		{
			fprintf(fOutputStream, "\t\t<T N=\"%d\" W=\"", iI);
			fwrite(aTerms[iI].term, aTerms[iI].term_length, 1, fOutputStream);
			fprintf(fOutputStream, "\" DN=\"%d\" DG=\"%d\"/>\n", aTerms[iI].doc_count, aTerms[iI].doc_group_count);
		}
		fprintf(fOutputStream, "\t</TL>\n");
		fprintf(fOutputStream, "\t<RL N=\"%d\">\n", iDocumentsCount);
	}

	/**
	  @brief Start new document and store document properties
	  @param pDocumentInfo - Information about document
	  @param dRank - document relevance
	*/
	void StartDocument(const DocumentInfo * pDocumentInfo, const W_FLOAT & dRank)
	{
		fprintf(fOutputStream, "\t\t<D N=\"%d\" RV=\"%f\">\n", ++iDocNum, dRank);
	}

	/**
	  @brief End of document
	*/
	void EndDocument() { fprintf(fOutputStream, "\t\t</D>\n"); }

	/**
	  @brief Start of snippet
	*/
	void StartSnippet() { fprintf(fOutputStream, "\t\t\t<S>"); }

	/**
	  @brief Add snippet data to document
	  @param sSnippet - snippet data
	  @param iLength - snippet data length
	*/
	void AddSnippetData(CCHAR_P sSnippet, const Size_t & iLength) { fwrite(sSnippet, 1, iLength, fOutputStream); }

	/**
	  @brief Add term to document
	  @param sSnippet - Term
	  @param iLength - Term length
	*/
	void AddTerm(CCHAR_P sSnippet, const Size_t & iLength)
	{
		fprintf(fOutputStream, "<B>");
		fwrite(sSnippet, 1, iLength, fOutputStream);
		fprintf(fOutputStream, "</B>");
	}

	/**
	  @brief End of snippet
	*/
	void EndSnippet() { fprintf(fOutputStream, "</S>\n"); }

	/**
	  @brief End of search result collection
	*/
	void EndOutput()
	{
		fprintf(fOutputStream, "\t</RL>\n");
		fprintf(fOutputStream, "</FTSSP>\n");
	}

	/**
	  @brief Store query execution time
	  @param iISec - number of seconds
	  @param iIUSec - number of microseconds
	*/
	void ExecTime(const UINT_64 & iISec, const UINT_64 & iIUSec)
	{
		iSec = iISec;
		iUSec = iIUSec;
	}

	/**
	  @brief Set error code
	  @param iErrorCode - unix error code
	*/
	void SetErrorCode(const INT_32 & iErrorCode)
	{
		;;
	}
};

int main(int argc, char ** argv)
{
	XMLOutputDataCollector oXMLCollector(stdout);
	OutputDataCollector * pCollector = &oXMLCollector;

	pCollector -> ExecTime(1, 0);
	pCollector -> StartOutput(4, NULL, 0);

	for (INT_32 iI = 0; iI < 4; ++iI)
	{
		DocumentInfo oDocInfo;
		pCollector -> StartDocument(&oDocInfo, rand());
		for (INT_32 iJ = 0; iJ < 3; ++iJ)
		{
			pCollector -> StartSnippet();

			char szBuffer[128];
			INT_32 iLen = snprintf(szBuffer, 127, "snippet test %d-%d", iI, iJ);
			pCollector -> AddSnippetData(szBuffer, iLen);

			iLen = snprintf(szBuffer, 127, " %d ", iJ);
			pCollector -> AddTerm(szBuffer, iLen);

			pCollector -> AddSnippetData("end", 3);

			pCollector -> EndSnippet();
		}
		pCollector -> EndDocument();
	}

	pCollector -> EndOutput();
}
// End.
